const router = require('koa-router')()
const dbs = require('../../mysql/index').isconnect()
router.get('/all', async ctx => {
  ctx.body = await dbs.find('*', 'new')
})

router.post('/add', async ctx => {
  let { title, content, tagtype } = ctx.request.body
  let time = new Date().getTime()
  if (title == undefined || content == undefined || tagtype == undefined) {
    ctx.body = { code: 1000, result: '请输入完整的数据' }
  } else {
    let res = await dbs.insert(
      'new',
      `('${title}','${content}','${tagtype}','${time}')`,
      'backstage'
    )
    if (res.code == 1) {
      ctx.body = { code: 200, result: '请求成功' }
    } else {
      ctx.body = { code: 1000, result: '请求失败，请检查输入数据' }
    }
  }
})

router.post('/', async ctx => {
  ctx.body = { code: 200, result: '测试请求成功' }
})

module.exports = router.routes()
